package com.uwaterloo.interviewme.am.udm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.uwaterloo.interviewme.common.DBConstants;
import com.uwaterloo.interviewme.model.DetailInfo;
import com.uwaterloo.interviewme.model.Interview;

/**
 * The Class SQLiteUserDataManager.
 */
public class SQLiteUserDataManager implements UserDataManager {

	/** The dbAdapter. */
	private InterviewDbAdapter iDbAdapter;

	/**
	 * Instantiates a new Sqlite user data manager.
	 *
	 * @param ctx the ctx
	 */
	public SQLiteUserDataManager(Context ctx) {
		iDbAdapter = new InterviewDbAdapter(ctx);
		iDbAdapter.open();
	}

	/* (non-Javadoc)
	 * @see com.uwaterloo.interviewme.am.udm.UserDataManager#saveInterview(com.uwaterloo.interviewme.model.Interview)
	 */
	@Override
	public boolean saveInterview(Interview interview) {
		return iDbAdapter.insertInterview(interview) > 0;
	}

	/* (non-Javadoc)
	 * @see com.uwaterloo.interviewme.am.udm.UserDataManager#updateInterview(com.uwaterloo.interviewme.model.Interview)
	 */
	@Override
	public boolean updateInterview(Interview interview) {
		return iDbAdapter.updateInterview(interview);
	}

	/* (non-Javadoc)
	 * @see com.uwaterloo.interviewme.am.udm.UserDataManager#retriveAllInterview()
	 */
	@Override
	public Collection<Interview> retriveAllInterview() {
		List<Interview> interviewSet = new ArrayList<Interview>();

		Cursor c = iDbAdapter.fetchAllInterviews();
		c.moveToFirst();

		int idIndex = c.getColumnIndex(DBConstants.ID);
		int cnameIndex = c.getColumnIndex(DBConstants.CNAME);
		int posIndex = c.getColumnIndex(DBConstants.POSITION);
		int locIndex = c.getColumnIndex(DBConstants.LOCATION);
		int timeIndex = c.getColumnIndex(DBConstants.TIME);
		while (!c.isAfterLast()) {
			interviewSet.add(new Interview(c.getString(idIndex), c
					.getString(cnameIndex), c.getString(posIndex), c
					.getString(locIndex), c.getString(timeIndex)));
			c.moveToNext();
		}
		c.close();
		return interviewSet;
	}

	/* (non-Javadoc)
	 * @see com.uwaterloo.interviewme.am.udm.UserDataManager#retriveInterview(java.lang.String)
	 */
	@Override
	public Interview retriveInterview(String interviewID) {
		Cursor c = iDbAdapter.fetchInterview(interviewID);
		int idIndex = c.getColumnIndex(DBConstants.ID);
		int cnameIndex = c.getColumnIndex(DBConstants.CNAME);
		int posIndex = c.getColumnIndex(DBConstants.POSITION);
		int locIndex = c.getColumnIndex(DBConstants.LOCATION);
		int timeIndex = c.getColumnIndex(DBConstants.TIME);

		Interview interview = new Interview(c.getString(idIndex), c
				.getString(cnameIndex), c.getString(posIndex), c
				.getString(locIndex), c.getString(timeIndex));

		int typeIndex = c.getColumnIndex(DBConstants.TYPE);
		String type = c.getString(typeIndex);

		if (!DBConstants.NA.equals(type)) {
			int statusIndex = c.getColumnIndex(DBConstants.STATUS);
			int lenIndex = c.getColumnIndex(DBConstants.LENGTH);
			int jdIndex = c.getColumnIndex(DBConstants.JDESC);

			DetailInfo detailInfo = new DetailInfo(type, c
					.getString(statusIndex), c.getString(lenIndex), c
					.getString(jdIndex));
			interview.setDetailInfo(detailInfo);
		}

		c.close();
		return interview;

	}

	/* (non-Javadoc)
	 * @see com.uwaterloo.interviewme.am.udm.UserDataManager#deleteInterview(java.lang.String)
	 */
	@Override
	public boolean deleteInterview(String interviewID) {
		return iDbAdapter.deleteInterview(interviewID);
	}
}
